﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IWMAG.Scripting.AST.ObjectModel;
using IWMAG.Scripting.Types;
using IWMAG.Scripting.AST.Declarations;

namespace IWMAG.Scripting.AST {
    public struct Signature {
        public CodeType ReturnType;
        public string Name;
        public VariableDeclaration[] Parameters;
        public bool IsExtern;
        public bool IsAsync;

        public Signature(string name, CodeType ret, IEnumerable<VariableDeclaration> param) {
            Name = name;
            ReturnType = ret;
            IsExtern = false;
            IsAsync = false;

            List<VariableDeclaration> temp = new List<VariableDeclaration>(param);
            Parameters = temp.ToArray();
        }

        // We don't care about the identifiers, but they're useful
        // to carry around anyway
        public override bool Equals(object obj) {
            if (!(obj is Signature)) {
                return false;
            }

            Signature b = (Signature) obj;
            if (Parameters.Length != b.Parameters.Length) {
                return false;
            }

            for (int i = 0; i < Parameters.Length; i++) {
                if (Parameters[i].Type != b.Parameters[i].Type) {
                    return false;
                }
            }

            return ReturnType == b.ReturnType;
        }

        public override int GetHashCode() {
            int hash = ReturnType.GetHashCode();
            for (int i = 0; i < Parameters.Length; i++) {
                hash += Parameters[i].Type.GetHashCode();
            }

            return hash;
        }
    }
}
